package com.hst.capacity.facade.rest;

import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.TokenBiz;
import com.hst.capacity.application.export.XssfExporter;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.export.OrderExportParam;
import com.hst.capacity.domain.model.request.finance.OrderStatementParam;
import com.hst.capacity.domain.model.request.invoice.InvoiceLstParam;
import com.hst.capacity.infra.util.DateUtil;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

@RequestMapping("/export")
@RestController
public class ExporterController {

    @Autowired
    XssfExporter xssfExporter;

    @Autowired
    TokenBiz tokenBiz;

    private String fileName;

    /**
     * 订单列表导出Excel
     */
    @PostMapping("/order-list")
    public ResponseEntity<ByteArrayResource> exportOrderList(@RequestBody OrderExportParam param, HttpServletRequest request){
        param.setIdUser(tokenBiz.findIdUser(request));
        Workbook workbook = xssfExporter.exportOrderList(param); // creates the workbook
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            workbook.write(stream);
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileName = String.format("OrderList_%s.xlsx", DateUtil.getNowString());
        return new ResponseEntity<>(new ByteArrayResource(stream.toByteArray()),
                headers(fileName),
                HttpStatus.CREATED);
    }

    /**
     * 对账明细记录导出Excel
     */
    @PostMapping("/statement-list")
    public ResponseEntity<ByteArrayResource> exportOrderStatement(@RequestBody OrderStatementParam param, HttpServletRequest request){
        param.setIdUser(tokenBiz.findIdUser(request));
        Workbook workbook = xssfExporter.exportOrderStatement(param);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            workbook.write(stream);
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileName = String.format("OrderStatement_%s.xlsx", DateUtil.getNowString());
        return new ResponseEntity<>(new ByteArrayResource(stream.toByteArray()),
                headers(fileName),
                HttpStatus.CREATED);
    }

    /**
     * 订单列表导出Excel
     */
    @PostMapping("/journeyList")
    public ResponseEntity<ByteArrayResource> exportJourneyList(@RequestBody Map<String,String> param) {
        String idInvoice = param.get("idInvoice");
        if (StrUtil.isEmpty(idInvoice))
            throw new BusinessException(4001,"发票ID[idInvoice]不能为空");
        Workbook workbook = xssfExporter.exportJourneyList(idInvoice); // creates the workbook
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            workbook.write(stream);
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileName = String.format("travel_%s.xlsx", DateUtil.getNowString());
        return new ResponseEntity<>(new ByteArrayResource(stream.toByteArray()),
                headers(fileName),
                HttpStatus.CREATED);
    }

    /**
     * 发票列表导出Excel
     */
    @PostMapping("/invoiceList")
    public ResponseEntity<ByteArrayResource> exportInvoiceList(@RequestBody InvoiceLstParam param, HttpServletRequest request) {
        param.setIdFinance(tokenBiz.findIdUser(request));
        Workbook workbook = xssfExporter.exportInvoiceList(param); // creates the workbook
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            workbook.write(stream);
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileName = String.format("invoice_%s.xlsx", DateUtil.getNowString());
        return new ResponseEntity<>(new ByteArrayResource(stream.toByteArray()),
                headers(fileName),
                HttpStatus.CREATED);
    }

    private HttpHeaders headers(String fileName) {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(new MediaType("application", "force-download"));
        header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
        return header;
    }
    
}
